﻿using System;
using UnityEngine;
using Framework.Manager;

namespace Framework.UI
{
    public class UIButton : UIBase
    {
        [SerializeField]
        private string mSoundName = "Sound_ButtonClick";
        private ExButton mExButton = null;


        /// <summary>注册点击缓存</summary>
        private Action mOnClick = null;
        /// <summary>注册双击缓存</summary>
        private Action mOnDoubleClick = null;
        /// <summary>注册长按开始缓存</summary>
        private Action mOnPressBegin = null;
        /// <summary>注册长按缓存</summary>
        private Action mOnPress = null;
        /// <summary>注册长按结束缓存</summary>
        private Action mOnPressEnd = null;

        public Action OnClick
        {
            set
            {
                mOnClick = value;
                if (value != null)
                    mExButton.OnClick = OnClickButton;
                else
                    mExButton.OnClick = null;
            }
        }
        public Action OnDoubleClick
        {
            set
            {
                mOnDoubleClick = value;
                if (value != null)
                    mExButton.OnDoubleClick = OnDoubleClickButton;
                else
                    mExButton.OnDoubleClick = null;
            }
        }
        public Action OnPressBegin
        {
            set
            {
                mOnPressBegin = value;
                if (value != null)
                    mExButton.OnPressBegin = OnPressBeginButton;
                else
                    mExButton.OnPressBegin = null;
            }
        }
        public Action OnPress
        {
            set
            {
                mOnPress = value;
                if (value != null)
                    mExButton.OnPress = OnPressButton;
                else
                    mExButton.OnPress = null;
            }
        }
        public Action OnPressEnd
        {
            set
            {
                mOnPressEnd = value;
                if (value != null)
                    mExButton.OnPressEnd = OnPressEndButton;
                else
                    mExButton.OnPressEnd = null;
            }
        }

        public string SoundName
        {
            get { return mSoundName; }
            set { mSoundName = value; }
        }


        protected override void OnAwake()
        {
            mExButton = GetComponent<ExButton>("ExButton");
        }

        private void OnClickButton()
        {
            ManagerCollection.AudioManager.PlaySound(mSoundName);
            mOnClick?.Invoke();
        }

        private void OnDoubleClickButton()
        {
            ManagerCollection.AudioManager.PlaySound(mSoundName);
            mOnDoubleClick?.Invoke();
        }

        private void OnPressBeginButton()
        {
            mOnPressBegin?.Invoke();
        }

        private void OnPressButton()
        {
            mOnPress?.Invoke();
        }

        private void OnPressEndButton()
        {
            ManagerCollection.AudioManager.PlaySound(mSoundName);
            mOnPressEnd?.Invoke();
        }

        #region 编辑器相关
        override protected void OnValidate()
        {
            base.OnValidate();
        }
        #endregion
    }
}